package com.td.teach;

import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;

/**
 * Created by luohaolun on 2017/11/30.
 */

public class CustomTransformer implements ViewPager.PageTransformer {


    private static final float MIN_SCALE = 0.5f;
    private final float sc = 0.8f;
    private final float sc_w = 0.6f;

    final float SCALE_MAX = 0.8f;
    final float ALPHA_MAX = 0.5f;

    @Override
    public void transformPage(View view, float position) {
        int pageWidth = view.getWidth();
        float width = pageWidth / 2.5f;

        if (position < -1) { // [-Infinity,-1)
            // This page is way off-screen to the left.
//            view.setAlpha(0);
            view.setScaleX(MIN_SCALE );
            view.setScaleY(MIN_SCALE );
            view.setTranslationX(width);

        } else if (position < 0) { // [-1,0]
            view.setAlpha(1);
            float s = (1 - Math.abs(position)) * (sc - MIN_SCALE);
            float s_w = (1 - Math.abs(position)) * (sc_w - MIN_SCALE);

            view.setScaleX(MIN_SCALE + s_w);
            view.setScaleY(MIN_SCALE + s);

            // pageWidth /2 ~ 0
            float w = width - (1 - Math.abs(position)) * width;

            view.setTranslationX(w);


        } else if (position == 0) {
            view.setAlpha(1);
            view.setScaleX(sc_w);
            view.setScaleY(sc);
            view.setTranslationX(0f);
        } else if (position <= 1) { // (0,1]

            view.setAlpha(1);
            float s = (1 - Math.abs(position)) * (sc - MIN_SCALE);
            float s_w = (1 - Math.abs(position)) * (sc_w - MIN_SCALE);
            view.setScaleX(MIN_SCALE + s_w);
            view.setScaleY(MIN_SCALE + s);

            // pageWidth /2 ~ 0
            float w = (1 - Math.abs(position)) * width - width;
            view.setTranslationX(w);

        } else { // (1,+Infinity]
            // This page is way off-screen to the right.
//            view.setAlpha(0);
            view.setScaleX(MIN_SCALE );
            view.setScaleY(MIN_SCALE );
            view.setTranslationX(-width);

        }


        //        float scale = (position < 0)
        //                ? ((1 - SCALE_MAX) * position + 1)
        //                : ((SCALE_MAX - 1) * position + 1);
        //        float alpha = (position < 0)
        //                ? ((1 - ALPHA_MAX) * position + 1)
        //                : ((ALPHA_MAX - 1) * position + 1);
        //        //为了滑动过程中，page间距不变，这里做了处理
        //        if(position < 0) {
        //            ViewCompat.setPivotX(page, page.getWidth());
        //            ViewCompat.setPivotY(page, page.getHeight() / 2);
        //        } else {
        //            ViewCompat.setPivotX(page, 0);
        //            ViewCompat.setPivotY(page, page.getHeight() / 2);
        //        }
        //        ViewCompat.setScaleX(page, scale);
        //        ViewCompat.setScaleY(page, scale);
        //        ViewCompat.setAlpha(page, Math.abs(alpha));
    }


}
